package com.drophair.jspwork.Servlet;

import com.drophair.jspwork.bean.SqlConnect;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Vector;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String uname="";
        String upwd="";
        String upwd1="";
        uname=request.getParameter("register_name");
        upwd=request.getParameter("register_pwd");
        upwd1=request.getParameter("register_pwd1");

        SqlConnect sqlConnect=new SqlConnect();

        Vector<String> vector=new Vector<String>();

        System.out.println(uname + " " + upwd + " " + upwd1);
        if(upwd==null||upwd1==null||upwd1.equals("")|| upwd1.equals("")){
            request.setAttribute("error","密码不得为空");
            request.getRequestDispatcher("register.jsp").forward(request,response);
        }
        else if(!upwd.equals(upwd1)){
            request.setAttribute("error","密码不一致");
            request.getRequestDispatcher("register.jsp").forward(request,response);
        }else{
            try {
                vector = sqlConnect.register(uname,upwd);
                request.setAttribute("error","注册成功，快来登陆吧");
                request.getRequestDispatcher("login.jsp").forward(request,response);
//                request.setAttribute("");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
    }
}
